/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.filters;

import controllers.pipes.Persistence2SenderPipe;
import controllers.pipes.Reader2ValidatorPipe;
import controllers.pipes.Validator2PersistencePipe;
import dao.ArchiveDAO;
import dao.DocumentTypeDAO;
import dao.FranchiseDAO;
import dao.TraceStateDAO;
import entities.*;
import facade.facadeClass;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import ws.serviceNoSyntax.NotSyntaxTransactionVO;

/**
 *
 * @author Sergio
 */
public final class SenderFilter extends Thread {

  private long lines = 0;
  private Archive archive;
  private TraceStateDAO traceStateDao;
  private Trace entityTrace;
  private TraceState entityTraceState;
  private FranchiseDAO franchiseDAO;
  private DocumentTypeDAO documentTypeDAO;
  private DocumentType entityDocumentType;
  private Franchise entityFranchise;
  private facadeClass fc;
  private String checkSum;

  public void setArchive(Archive archive) {
    this.archive = archive;
  }

  public void setLines(long lines) {
    this.lines = lines;
  }

  public SenderFilter() {
    super("PersistenceFilter");
    System.out.println("Starting thread: " + this);
  }

  @Override
  public void run() {
    try {
      Persistence2SenderPipe theInQueueInstance = Persistence2SenderPipe.getInstance();
      while (lines == 0) {
        Thread.sleep(200);//number of lines has not been initialized, wait some time.
      }
      for (int i = 0; i < lines;) {
        String currentWord = theInQueueInstance.getElement();//read from the pipe.
        if (currentWord == null) {
          System.out.println("There is nothing here!! PERSISTENCE");//nothing in the pipe. Wait.
        } else {
          System.out.println(currentWord + " in the sender!!");
          //send to persistence
          i++;//go to the next element in the pipa
          Send(currentWord, i);
        }
        Thread.sleep(200);//wait
      }
    } catch (InterruptedException e) {
      System.out.println("Child interrupted.");
    }
    System.out.println("Exiting (consumer).");
  }

  public void Send(String line, int count) {
    String[] tempTrace;
    String delimiter = "\\|";
    tempTrace = line.split(delimiter);
    if ("0".equals(tempTrace[0])) {
      String[] tTrace = new String[9];
      for (int j = 0; j < tempTrace.length - 1; j++) {
        System.out.println(tempTrace[j + 1]);
        tTrace[j] = tempTrace[j + 1];
      }
      sendValid(tTrace);
    } else {
      sendInvalid();
    }
  }

  public void sendInvalid() {

    NotSyntaxTransactionVO newNotSyntax = new NotSyntaxTransactionVO();

    String uno = "4fa70fae46efad42ad423cb0";
    String dos = "4fa70fae46efad42ad423cd0";

    String idArchive = "4fa70fae46efad42ad423cb0";

    long line = 45354;
    String cinco = String.valueOf(line);

    TimeZone timeZone1 = TimeZone.getTimeZone("America/Bogota");

    Calendar calendar = new GregorianCalendar();
    calendar.setTimeZone(timeZone1);
    String seis = calendar.getTime().toString();

    // String seis= (new Date()).toString();

    newNotSyntax.setBrand(uno);
    newNotSyntax.setEstablishment(dos);
    newNotSyntax.setFileId(idArchive);

    newNotSyntax.setTransactionLine(cinco);
    newNotSyntax.setValidationDate(seis);

    createNotSyntax(newNotSyntax);
  }

  /**
   * This method is supposed to send vald traces to the mongo
   *
   * @param tempTrace
   * @return
   */
  public boolean sendValid(String[] tempTrace) {
    /**
     * implement here the sending logic!!
     */
    return true;
  }

  private static boolean createNotSyntax(ws.serviceNoSyntax.NotSyntaxTransactionVO newNotSyntax) {
    ws.serviceNoSyntax.WebServiceNotSyntax_Service service = new ws.serviceNoSyntax.WebServiceNotSyntax_Service();
    ws.serviceNoSyntax.WebServiceNotSyntax port = service.getWebServiceNotSyntaxPort();
    return port.createNotSyntax(newNotSyntax);
  }
}